package com.nbgroup.service.impl;

import com.nbgroup.pojo.common.LoginInfo;
import com.nbgroup.pojo.vo.LoginVO;
import com.nbgroup.service.LoginService;
import com.nbgroup.service.UserService ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private LoginService loginService;

    @Override
    public LoginVO login(LoginInfo loginInfo) {
        LoginVO loginVO = new LoginVO();
        String realCode = redisTemplate.opsForValue().get(loginInfo.getUuid());
        // 得到用户输入的验证码
        String code = loginInfo.getCode();

        // 得到的验证码为空，则获取验证码到登录之间的时间已经过了3分钟，验证码过期已经被删除
        if (realCode == null) {
            loginVO.setCode(500);
            loginVO.setMsg("验证码不存在");
            return loginVO;
        }
        // 验证码校验
        if (!realCode.equals(code)) {
            loginVO.setCode(500);
            loginVO.setMsg("验证码错误");
            return loginVO;
        }
        return loginService.login(loginInfo);
    }
}
